package com.wpx.log.core.handler;

import java.lang.reflect.Field;
import com.wpx.log.core.LogContext;
import com.wpx.log.core.LogRequestContext;
import com.wpx.log.core.handler.status.ILogStatusPolicy;
import com.wpx.log.core.module.DefaultLog;

public class OpStatusLogHandler implements ILogHandler {

	@Override
	public Object handler(LogContext logContext, LogRequestContext logRequestContext, Field logField) {
		if(logRequestContext.isIpPass() == false) {
			return DefaultLog.IP_NOT_ALLOW;
		}
		
		Throwable e = logRequestContext.getException();
		if(logRequestContext.getException() != null) {
			ILogStatusPolicy statusPolicy = logContext.getLogStatusPolicy(e.getClass());
			if(statusPolicy != null) {
				return statusPolicy.getStatus(e);
			}
			return DefaultLog.RESULT_ERROR;
		}
		
		Object returnObj = logRequestContext.getReturnObj();
		
		if(returnObj == null) {
			return DefaultLog.RESULT_SUCCESS;
		}
		
		ILogStatusPolicy statusPolicy = logContext.getLogStatusPolicy(returnObj.getClass());
		if(statusPolicy != null) {
			return statusPolicy.getStatus(returnObj);
		}		
		
		return DefaultLog.RESULT_SUCCESS;
	}

}
